package com.shengqi.xinnongren.work.ui.user.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.user.model.CollectionBean;
import com.shengqi.xinnongren.work.ui.user.view.MyCollectionView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MyCollectionPresenter extends BasePresenter<MyCollectionView> {

    public void getCollectionList() {
        getView().showLoadingView();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), Constants.getAESJsonData(new HashMap<>(), UserManager.getInstance().getToken()).toString());
        addTask(RetrofitHelper.getInstance().getService().getCollectionList(requestBody), respond -> {
            getView().hideLoadingView();
            HttpRespond<String> httpResult = Global.getHttpResult(respond);
            if (httpResult.result == 1) {
                List<CollectionBean> list = new Gson().fromJson(httpResult.data, new TypeToken<List<CollectionBean>>() {
                }.getType());
                getView().showCollectionList(list);
            } else
                getView().onRequestFailed(httpResult.message);
        });
    }

    public void delCollection(String maId) {
        Map<String, Object> map = new HashMap<>();
        map.put("comId", maId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().delCollection(requestBody), respond -> {
            if (respond.result == 1) {
                getView().showDesCollection(respond.message);
            } else
                getView().onRequestFailed(respond.message);
        });
    }

}
